var app = getApp()
var getAppGlobalData = require('../../../templates/copyright/copyright.js');
Page({
  data: {
    type: '',
    name: '',
    identification: '',
    globalData: {},
    companyData: {
      list: {}
    },
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    console.log(this);
    var that = this;
    getAppGlobalData.getAppGlobalData(that);
    if (options) {
      wx.setNavigationBarTitle({
        title: options.name
      })
      that.setData({
        type: options.type,
        name: options.name,
        identification: options.identification
      })
    }

    if (options.to_uid) {
      app.globalData.to_uid = that.data.to_uid;
    }
    if (options.from_id) {
      app.globalData.from_id = that.data.from_id;
    }

    if (wx.getStorageSync("formID")) {
      let formID = wx.getStorageSync("formID");
      formID = that.data.from_id;
      wx.setStorageSync("formID", formID);
    } else {
      wx.setStorageSync("formID", that.data.from_id);
    }
    that.getContactData();
  },
  onReady: function () {
    // 页面渲染完成
    var that = this;
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  onPullDownRefresh: function () {
    // 监听用户下拉动作
  },
  onReachBottom: function () {
    // 页面上拉触底
  },
  onShareAppMessage: function (res) {
    // 用户点击右上角分_享
    var that = this;
    if (res.from === 'button') {
      console.log("来自页面内转发按钮");
    }
    else {
      console.log("来自右上角转发菜单", that.data.name)
    }
    return {
      title: '',
      path: '/longbing_card/pages/company/contact/contact?to_uid=' + app.globalData.to_uid + '&from_id=' + wx.getStorageSync("userid") + '&type=' + that.data.type + '&name=' + that.data.name + '&identification=' + that.data.identification,
      imageUrl: ''
    };
  },
  getContactData: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/modular',
      'cachetime': '30',
      'method': 'POST',
      'data': {
        to_uid: app.globalData.to_uid
      },
      success: function (res) {
        console.log("entry/wxapp/modular ==>", res, that.data.globalData.company.name)
        if (!res.data.errno) {


          that.setData({
            markers: [{
              iconPath: "/longbing_card/resource/images/map.png",
              id: 1,
              callout: {
                content: that.data.globalData.company.name,
                fontSize: 14,
                bgColor: '#ffffff',
                padding: 4,
                display: 'ALWAYS',
                textAlign: 'center',
                borderRadius: 2,
              },
              latitude: that.data.globalData.company.latitude,
              longitude: that.data.globalData.company.longitude,
              width: 30,
              height: 30
            }]
          })


          let tmpData = res.data.data;
          for (let i in tmpData) {
            if (tmpData[i].identification == that.data.identification && tmpData[i].type == that.data.type) {

              var tmpDDDD = [];
              tmpDDDD.push(tmpData[i]);

              that.setData({
                companyData: tmpDDDD
              })

            }
          }
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  regionchange(e) {
    console.log(e.type)
  },
  markertap(e) {
    console.log(e.markerId)
  },
  controltap(e) {
    console.log(e.controlId)
  },
  toJump: function (e) {
    var that = this;
    var status = e.currentTarget.dataset.status;
    var content = e.currentTarget.dataset.content;
    if (status == 'toCall') {
      if (!content || content == '尚未填写') {
        return false;
      }
      wx.makePhoneCall({
        phoneNumber: content,
        success: function (res) {
          // console.log('拨打电话成功 ==>>', res.data);
          if (app.globalData.to_uid != wx.getStorageSync("userid")) {
            that.toCopyRecord(type);
          }
        }
      });
    }
  }
})